"""
优化版智能招聘助手Agent使用示例
"""

from optimized_recruitment_agent import (
    create_optimized_agent, 
    EnhancedResumeParser, 
    EnhancedJobMatcher, 
    EnhancedMessageGenerator,
    RecruitmentConfig,
    ResumeInfo
)


def demo_basic_usage():
    """基本使用示例"""
    print("=" * 60)
    print("智能招聘助手Agent - 基本使用示例")
    print("=" * 60)
    
    # 创建Agent
    agent = create_optimized_agent()
    
    # 测试简历
    resume_text = """
    姓名：张三
    工作经验：5年
    技能：Python、LangChain、Agent开发、大模型微调、RAG
    项目：主导开发过企业级AI客服Agent系统，使用LangChain实现工具调用逻辑
    教育背景：计算机科学硕士
    联系方式：zhangsan@email.com
    """
    
    print("输入简历：")
    print(resume_text)
    print("\n" + "=" * 60)
    
    # 处理简历
    try:
        result = agent.invoke({"input": resume_text})
        print("Agent处理结果：")
        print(result["output"])
    except Exception as e:
        print(f"处理失败: {e}")


def demo_component_usage():
    """组件使用示例"""
    print("\n" + "=" * 60)
    print("组件使用示例")
    print("=" * 60)
    
    # 创建组件
    config_manager = RecruitmentConfig()
    parser = EnhancedResumeParser()
    matcher = EnhancedJobMatcher(config_manager)
    generator = EnhancedMessageGenerator()
    
    # 测试简历
    resume_text = """
    姓名：李四
    工作经验：3年
    技能：Python、Django、MySQL、基础机器学习
    项目：参与开发电商后台管理系统，负责订单模块
    """
    
    print("输入简历：")
    print(resume_text)
    print("\n" + "-" * 40)
    
    # 1. 解析简历
    print("1. 简历解析：")
    resume_info = parser.parse_resume(resume_text)
    print(f"   姓名: {resume_info.name}")
    print(f"   经验: {resume_info.experience_years}年")
    print(f"   技能: {', '.join(resume_info.skills)}")
    print(f"   项目: {', '.join(resume_info.projects)}")
    
    # 2. 计算匹配度
    print("\n2. 岗位匹配：")
    match_result = matcher.calculate_match(resume_info, "ai_agent_engineer")
    print(f"   匹配分数: {match_result.score:.1f}分")
    print(f"   匹配等级: {match_result.level.value}")
    print(f"   匹配技能: {', '.join(match_result.matched_skills)}")
    print(f"   缺失技能: {', '.join(match_result.missing_skills)}")
    print(f"   经验匹配: {match_result.experience_match}")
    print(f"   建议: {match_result.recommendation}")
    
    # 3. 生成话术
    print("\n3. 话术生成：")
    message = generator.generate_message(match_result, "AI Agent开发工程师", "李四")
    print(message)


def demo_multiple_jobs():
    """多岗位匹配示例"""
    print("\n" + "=" * 60)
    print("多岗位匹配示例")
    print("=" * 60)
    
    # 创建组件
    config_manager = RecruitmentConfig()
    parser = EnhancedResumeParser()
    matcher = EnhancedJobMatcher(config_manager)
    
    # 测试简历
    resume_text = """
    姓名：王五
    工作经验：4年
    技能：Python、Django、Flask、MySQL、Redis、Docker
    项目：主导开发过微服务架构的电商平台，使用Docker容器化部署
    """
    
    print("输入简历：")
    print(resume_text)
    print("\n" + "-" * 40)
    
    # 测试不同岗位
    job_types = ["ai_agent_engineer", "python_developer", "data_scientist"]
    
    for job_type in job_types:
        try:
            config = config_manager.get_job_config(job_type)
            resume_info = parser.parse_resume(resume_text)
            match_result = matcher.calculate_match(resume_info, job_type)
            
            print(f"\n{config.job_title} 匹配结果：")
            print(f"   分数: {match_result.score:.1f}分")
            print(f"   等级: {match_result.level.value}")
            print(f"   匹配技能: {', '.join(match_result.matched_skills)}")
            
        except Exception as e:
            print(f"   {job_type} 匹配失败: {e}")


def demo_custom_config():
    """自定义配置示例"""
    print("\n" + "=" * 60)
    print("自定义配置示例")
    print("=" * 60)
    
    from optimized_recruitment_agent import JobConfig, MatchLevel
    
    # 创建自定义岗位配置
    custom_config = JobConfig(
        job_title="全栈工程师",
        required_skills=["Python", "React", "Node.js"],
        preferred_skills=["TypeScript", "Docker", "AWS", "MongoDB"],
        min_experience=3,
        max_experience=7,
        skill_weights={
            "Python": 0.25,
            "React": 0.25,
            "Node.js": 0.25,
            "TypeScript": 0.15,
            "Docker": 0.10
        },
        experience_weight=0.2,
        threshold_scores={
            MatchLevel.HIGH: 75,
            MatchLevel.MEDIUM: 55,
            MatchLevel.LOW: 0
        },
        job_description="负责全栈应用开发，包括前端、后端和部署"
    )
    
    # 添加配置
    config_manager = RecruitmentConfig()
    config_manager.add_job_config("fullstack_engineer", custom_config)
    
    print("已添加自定义岗位配置：全栈工程师")
    print(f"必需技能: {', '.join(custom_config.required_skills)}")
    print(f"优先技能: {', '.join(custom_config.preferred_skills)}")
    print(f"经验要求: {custom_config.min_experience}-{custom_config.max_experience}年")
    
    # 测试自定义岗位
    resume_text = """
    姓名：赵六
    工作经验：4年
    技能：Python、React、Node.js、TypeScript、Docker、AWS
    项目：开发过多个全栈应用，包括电商平台和内容管理系统
    """
    
    parser = EnhancedResumeParser()
    matcher = EnhancedJobMatcher(config_manager)
    
    resume_info = parser.parse_resume(resume_text)
    match_result = matcher.calculate_match(resume_info, "fullstack_engineer")
    
    print(f"\n全栈工程师匹配结果：")
    print(f"   分数: {match_result.score:.1f}分")
    print(f"   等级: {match_result.level.value}")
    print(f"   匹配技能: {', '.join(match_result.matched_skills)}")


def demo_error_handling():
    """错误处理示例"""
    print("\n" + "=" * 60)
    print("错误处理示例")
    print("=" * 60)
    
    parser = EnhancedResumeParser()
    
    # 测试各种异常情况
    test_cases = [
        ("空简历", ""),
        ("格式错误简历", "我叫张三，会编程"),
        ("None输入", None),
        ("特殊字符", "姓名：张三\n技能：Python@#$%"),
    ]
    
    for case_name, resume_text in test_cases:
        print(f"\n测试: {case_name}")
        try:
            result = parser.parse_resume(resume_text)
            print(f"   解析成功: {result.name}, {result.experience_years}年经验")
        except Exception as e:
            print(f"   解析失败: {e}")


if __name__ == "__main__":
    # 运行所有示例
    demo_basic_usage()
    demo_component_usage()
    demo_multiple_jobs()
    demo_custom_config()
    demo_error_handling()
    
    print("\n" + "=" * 60)
    print("所有示例运行完成！")
    print("=" * 60)
